/* 
 * This code is part of the smartlink project, see
 * 
 * 		http://code.google.com/p/smartlink/.
 * 
 * Copyright (C) 2008 Oliver Noelle
 * 
 * SmartLink is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * SmartLink is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with SmartLink.  If not, see <http://www.gnu.org/licenses/>.
 **/
package com.google.contactswrapper;

import org.smartlink.Server;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

/**
 * Thin wrapper around the contacts application to smartlink-enable it.
 */
public class ContactsWrapper extends Activity {

	private static final String TAG = "ContactsWrapper";
	/** request code for picking a target item */
	protected static final int PICK_TARGET_REQUEST_CODE = 1;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
	}

	@Override
	protected void onStart() {
		super.onStart();
		Intent intent = getIntent();
		String action = intent.getAction();
		if (action == null) {
			// eg. "launched" via eclipse
			finish();
			return;
		}
		if (action.equals(Server.PICK_TARGET_ACTION)) {
			Uri uri = Uri.parse("content://contacts/people");
			Intent targetIntent = new Intent(Intent.PICK_ACTION, uri);
			startSubActivity(targetIntent, PICK_TARGET_REQUEST_CODE);
		} else {
			Log.w(TAG, "warning: unexpected action: " + action);
			return;
		}
	}

	/** Listen for results of picking targets. */
	protected void onActivityResult(int requestCode, int resultCode, String data, Bundle extras) {
		if (resultCode == RESULT_CANCELED) {
			// picking a contact was canceled
			setResult(RESULT_CANCELED);
			finish();
			return;
		}
		switch (requestCode) {
		case PICK_TARGET_REQUEST_CODE:
			Uri uri = Uri.parse(data);
			// a smartlink server app has to provide two strings as a result of
			// picking a target:
			// 1. the target uri of the data item
			// 2. a descriptive text of the data item
			Bundle bundle = new Bundle(2);
			bundle.putString(Server.EXTRA_ITEM_TARGET, data);
			bundle.putString(Server.EXTRA_ITEM_DESCRIPTION, getName(uri));
			setResult(RESULT_OK, data, bundle);
			finish();
		default:
			Log.w(TAG, "unexpected result code: " + resultCode);
		}
	}

	private String getName(Uri uri) {
		return getColumn(uri, android.provider.Contacts.PeopleColumns.NAME);
	}

	/** gets the specified column of the contact database entry. */
	private String getColumn(Uri uri, String column) {
		String[] projection = new String[] { column };
		Cursor cur = managedQuery(uri, projection, // Which columns to return.
				null, // WHERE clause
				null); // Order-by clause.
		if (!cur.first()) {
			return null;
		}
		return cur.getString(cur.getColumnIndex(column));
	}

}
